#parse argument

architecture=$1
matrix_decompressed_file=$2
matrix_comp_file=$3
output_dir=$4
lorem_ipsum_file=$5

permutation_str=""


# permute xmatrix compressed random

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --encoding-conf -compiled-param -no-password \
    --sequence-conf -xmatrix-comp $matrix_comp_file \
    --permutation-conf -arch $architecture \
    --permute-xmatrix-comp-random -xmatrix-comp-dst $output_dir/xmatrix_comp_rnd.xf
"
echo ""; echo $str; echo ""
eval $str

# check if the xmatrix compressed permuted randomly is different

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --permutation-conf -arch $architecture \
    --logging-conf -lib-info-log $output_dir/xmatrix_comp_rnd.json \
    --print-header -xrn-file $output_dir/xmatrix_comp_rnd.xf
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --permutation-conf -arch $architecture \
    --logging-conf -lib-info-log $output_dir/xmatrix_comp.json \
    --print-header -xrn-file $matrix_comp_file
"
echo ""; echo $str; echo ""
eval $str

str=" 
cmp --silent $output_dir/xmatrix_comp.json $output_dir/xmatrix_comp_rnd.json && echo 'FAIL'
"
echo ""; echo $str; echo ""
eval $str

# permute xmatrix compressed deterministically 2 times

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --encoding-conf -compiled-param -no-password \
    --sequence-conf -xmatrix-comp $matrix_comp_file -xmatrix $matrix_decompressed_file \
    --permutation-conf -arch $architecture \
    --permute-xmatrix-comp-deterministic -xmatrix-comp-dst $output_dir/xmatrix_comp_det_0.xf
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --encoding-conf -compiled-param -no-password \
    --sequence-conf -xmatrix-comp $matrix_comp_file -xmatrix $matrix_decompressed_file \
    --permutation-conf -arch $architecture \
    --permute-xmatrix-comp-deterministic -xmatrix-comp-dst $output_dir/xmatrix_comp_det_1.xf
"
echo ""; echo $str; echo ""
eval $str

# check if the xmatrices compressed permuted deterministically are different

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --permutation-conf -arch $architecture \
    --logging-conf -lib-info-log $output_dir/xmatrix_comp_det_0.json \
    --print-header -xrn-file $output_dir/xmatrix_comp_det_0.xf
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --permutation-conf -arch $architecture \
    --logging-conf -lib-info-log $output_dir/xmatrix_comp_det_1.json \
    --print-header -xrn-file $output_dir/xmatrix_comp_det_1.xf
"
echo ""; echo $str; echo ""
eval $str

str=" 
cmp --silent $output_dir/xmatrix_comp_det_0.json $output_dir/xmatrix_comp_det_1.json || echo 'FAIL'
"
echo ""; echo $str; echo ""
eval $str

str=" 
cmp --silent $output_dir/xmatrix_comp_det_0.json $output_dir/xmatrix_comp.json && echo 'FAIL'
"
echo ""; echo $str; echo ""
eval $str

# permute xmatrix compressed confidentially 2 times

str="
xrnlib-cli \
    --encoding-conf -compiled-param -no-password \
    --permutation-conf -arch $architecture $permutation_str \
    --make-monomial-key -monomial-key $output_dir/monomial_key_file.xf 
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --encoding-conf -compiled-param -no-password \
    --arithmetic-conf -monomial-key $output_dir/monomial_key_file.xf \
    --sequence-conf -xmatrix-comp $matrix_comp_file -xmatrix $matrix_decompressed_file \
    --permutation-conf -arch $architecture \
    --permute-xmatrix-comp-confidential -xmatrix-comp-dst $output_dir/xmatrix_comp_sec_0.xf
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --encoding-conf -compiled-param -no-password \
    --arithmetic-conf -monomial-key $output_dir/monomial_key_file.xf \
    --sequence-conf -xmatrix-comp $matrix_comp_file -xmatrix $matrix_decompressed_file \
    --permutation-conf -arch $architecture \
    --permute-xmatrix-comp-confidential -xmatrix-comp-dst $output_dir/xmatrix_comp_sec_1.xf
"
echo ""; echo $str; echo ""
eval $str

# check if the xmatrices compressed permuted deterministically are different

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --permutation-conf -arch $architecture \
    --logging-conf -lib-info-log $output_dir/xmatrix_comp_sec_0.json \
    --print-header -xrn-file $output_dir/xmatrix_comp_sec_0.xf
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --permutation-conf -arch $architecture \
    --logging-conf -lib-info-log $output_dir/xmatrix_comp_sec_1.json \
    --print-header -xrn-file $output_dir/xmatrix_comp_sec_1.xf
"
echo ""; echo $str; echo ""
eval $str

str=" 
cmp --silent $output_dir/xmatrix_comp_sec_0.json $output_dir/xmatrix_comp_sec_1.json || echo 'FAIL'
"
echo ""; echo $str; echo ""
eval $str

str=" 
cmp --silent $output_dir/xmatrix_comp_sec_0.json $output_dir/xmatrix_comp.json && echo 'FAIL'
"
echo ""; echo $str; echo ""
eval $str

# check xmatrices

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --encoding-conf -compiled-param -no-password \
    --sequence-conf -xmatrix-comp $output_dir/xmatrix_comp_sec_0.xf \
    --permutation-conf -arch $architecture \
    --decompress-xmatrix -xmatrix $output_dir/xmatrix_dec_sec_0.xf
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --sequence-conf -xmatrix $output_dir/xmatrix_dec_sec_0.xf \
    --permutation-conf -arch $architecture \
    --logging-conf -lib-info-log $output_dir/lib_info_xmatrix_dec_sec_0.json \
    --check-xmatrix
"
echo ""; echo $str; echo ""
eval $str

str=" 
if ! [ -s $output_dir/lib_info_xmatrix_dec_sec_0.json ]; then echo FAIL ; exit ; fi ; \
if grep -q FAIL \"$output_dir/lib_info_xmatrix_dec_sec_0.json\" ; then echo FAIL ; exit ; fi
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --sequence-conf -xmatrix-comp $output_dir/xmatrix_comp_sec_0.xf \
    --permutation-conf -arch $architecture \
    --logging-conf -lib-info-log $output_dir/lib_info_xmatrix_comp_sec_0.json \
    --check-xmatrix-comp
"
echo ""; echo $str; echo ""
eval $str

str=" 
if ! [ -s $output_dir/lib_info_xmatrix_comp_sec_0.json ]; then echo FAIL ; exit ; fi ; \
if grep -q FAIL \"$output_dir/lib_info_xmatrix_comp_sec_0.json\" ; then echo FAIL ; exit ; fi
"
echo ""; echo $str; echo ""
eval $str


